#include "def.h"
#include "key_process.h"
#include "display.h"
#include "custom_define.h"
#include "string.h"
#include "buzzer.h"
//#include "wifi_comm.h"
//#include "save_date.h"
#include "comm_wifi.h"
#include "open_power_code.h"
#include "lcd_comm.h"
#include "def.h"

extern uint16_t wifi_ctrl_block_time;
Machine_Stat MachineStat=MODE_INIT;
Machine_Stat COMM_ERR_LAST_MODE=MODE_INIT;
Time wifi_send_cycle;
HEAT_FURNACE_F1_TXTE heat_furnace_f1_txte;
HEAT_FURNACE_F4_TXTE heat_furnace_f4_txte;
HEAT_FURNACE_F0_TXTE heat_furnace_f0_txte;

uint8_t ErrKeyBuzzerDelay = 0; /* Err2 报警时，关机按键声音延时*/
WIFI_RESERVATION wifi_reservation;
//extern uint8_t wifi_cmd;
//extern uint8_t cmd_times;
DATE_STAT date_stat;
extern uint8_t comm_stat;

uint8_t msg_type=0;
uint8_t dis_smartlink=0;
//uint8_t self_de=0;
//uint8_t self_in=0;
uint8_t dis_times = 0;
uint8_t key_flag=0;

uint8_t fire_err_stat=0;
uint8_t Err_Dis_Stat=0;
//用来做标记为，当它为1，表示MODE_INIT状��结束，�??以在dataflash�??进�?�取值，写��??
//防�?�程序一�?��?�，立即从datafataflash�??取��，写��??
uint8_t mode_change_flag=0;
uint16_t mode_save_temp[3]={DEF_PLAIN_TEMP, DEF_KITCHEN_TEMP, DEF_SHOWER_TEMP};
//uint8_t version_test_time=0;
extern uint8_t key_num;
uint8_t FactoryMode;
//extern uint8_t dis_cmd_times;

//extern uint8_t wifi_cmd;

Time timeout;
Time touchtime;
Time errtime;
Time wifitime;
Time versiontime;
Time akeycycle;
Time wifi_signal;
Time one_minute_version;


//用于计时，当时间大于等于6小时后，掉电记忆才会计时
Time six_hour;

Time cycle_time;
Time order_time; /* TODO: 测试用, 代替时钟 */
#ifdef OPEN_POWER_FLAG
Time openpower_time;
uint8_t open_power_max_time=OPEN_POWER_DEF_TIMEOUT;
uint16_t openpower_show_temp_time = 0; // ������������λ����ѭ������
#endif
uint8_t disakey_cycle=0;
uint8_t smartlink_start=0;
uint8_t success_wifi=0;
uint16_t Cycle_open_save_temp=0;
uint16_t  three_time=0;
uint8_t  three_3_flg=0;
//wifi_stat;
// uint8_t key_1;
// uint8_t key_2;
uint8_t cycle_flag=0;

HEATER_INFO heater_info;
//TIMES_STRUCT times_struct;
CYCLE_SWITCH cycle_switch;
CycleOrder_t CycleOrder;
uint16_t CycleDuration;     /* 一键循环时长 uint:minute*/
uint8_t CycleActiveSrc;
 
TIME_STAT time_stat;
extern uint8_t blink_flag_250ms;
extern uint8_t blink_flag_500ms;
extern uint8_t blink_flag_1000ms;
extern uint8_t blink_flag_2000ms;

extern uint16_t err_time_cnt;

extern uint8_t Commfail_Flag;

//extern uint8_t smartlink;
//extern WIFI_Stat wifi_stat;
Machine_Stat version_comm=MODE_TEST;
void BuzzerKeyFunc(void);
uint8_t err_flag=0;
extern uint8_t dis_runmode;

uint8_t OpenPowerLock = 0; /* 1：开机码超时锁定 */
uint8_t bBathtubeFinish = 0;
void machine_init(void)
{
        //TODO: 测试
    Get_Time(TIME_CLEAN, &order_time);
    
    Get_Time(TIME_CLEAN,&timeout);
    Get_Time(TIME_CLEAN,&touchtime);
    Get_Time(TIME_CLEAN,&errtime);
    Get_Time(TIME_CLEAN,&wifitime);
    Get_Time(TIME_CLEAN,&versiontime);
    Get_Time(TIME_CLEAN,&akeycycle);
    Get_Time(TIME_CLEAN,&wifi_signal);
    Get_Time(TIME_CLEAN,&one_minute_version);
	
	Get_Time(TIME_CLEAN,&cycle_time);
#ifdef OPEN_POWER_FLAG
	Get_Time(TIME_CLEAN,&openpower_time);
#endif
    memset(&heater_info,0,sizeof(heater_info));
	memset(&openpower_code,0,sizeof(openpower_code));
	memset(&time_stat,0,sizeof(time_stat));
	memset(&cycle_switch,0,sizeof(cycle_switch));
	//heater_info.running_mode=0x00;
	//heater_info.bathtub_mode=0x00;
	heater_info.akey_cycle = 0x30;
	heater_info.send_akey_cycle = 0x30;
	heater_info.super_mode = 0x30;
	heater_info.send_super_mode = 0x30;
	heater_info.power_stat = 0x30;
	heater_info.eco_stat = 0x30;
	heater_info.cool_and_hot = 0x30;
	heater_info.running_mode = RUN_PLAIN;
    
    FactoryMode = 0x30;
}
uint8_t num_change;
void machine_stat(void)
{
	static uint8_t MachineStat_old=MODE_INIT;
	static uint8_t mode_change;
	
//	display_wifi(0);
	button_process();
	//KeyProcess();

	if(one_minute_version.time_min<2)
	{
		Get_Time(UnitMin,&one_minute_version);
	}	
	if((EventListener(KEY_VERSION_LONG))&&(version_comm==MODE_TEST)&&(one_minute_version.time_min<2))//�??�?��?�接受的版本查�??
	{
		version_comm=MachineStat;
		MachineStat = MODE_VERSION;
		Get_Time(TIME_CLEAN,&versiontime);
		Buzzer_key();
	}
	 if(FactoryMode == 0x31 && MachineStat!=MODE_FACTORY_TEST && one_minute_version.time_min<2){
	    display_clean();
		MachineStat=MODE_FACTORY_TEST;
	 }
	if((MachineStat!=MODE_INIT)&&(MachineStat!=MODE_COMM_ERR))
	{
#ifdef OPEN_POWER_FLAG
		if(MachineStat!=MODE_OPEN_POWER)
		{
			Get_Time(UnitMin,&openpower_time);
		}
		//Aaron,����ѭ�����뵹��ʱ
		if(openpower_show_temp_time > 0) {
        openpower_show_temp_time--;
    }
#endif

		if((wifi_stat.smartlink==LINK_SUCCESS)&&(wifi_stat.success_link_wifi==LINK_FAILURE))
		{
			Get_Time(UnitSec,&wifitime);
			if(wifitime.time_sec>=120)
			{
				if(wifi_stat.success_link_wifi==LINK_FAILURE)
				{
					wifi_stat.smartlink=LINK_FAILURE;
				}
				Get_Time(TIME_CLEAN,&wifitime);
			}
		}	
		else
		{
			Get_Time(TIME_CLEAN,&wifitime);
		}
		wifi_display();
	}
	//if(one_minute_version.)
    switch(MachineStat)
    {
        case MODE_INIT: //0
			Get_Time(UnitMSec,&timeout);//Get_Time(UnitSec,&timeout);//timeout++;
			display_all();//Led_On();
			if(timeout.time_msec<10)
			    WIFI_Reset(ON);
			else
			    WIFI_Reset(OFF);	
			if(timeout.time_msec>150){
				
#ifndef OPEN_POWER_FLAG
				MachineStat=MODE_POWEROFF;
#else
				//if()
				MachineStat=MODE_RUN;
#endif
				Get_Time(TIME_CLEAN,&timeout);//timeout=0
				Get_Time(TIME_CLEAN,&wifi_signal);
			}
		break;
#ifdef OPEN_POWER_FLAG
		case MODE_OUT_OPEN_POWER:
		     if(heater_info.power_stat==0x30){
				key_flag=1;
			 	key_num=1;
				heater_info.power_stat=0x31;
			 }	  
		     MachineStat = MODE_RUN;     
		     break;	 
		case MODE_OPEN_POWER:
			
			if(heater_info.power_stat==0x31 && heater_info.fire_stat == 0){
				key_flag=1;
				key_num=1;
               heater_info.power_stat = 0x30;
			}
			// aaron,3lines,1.���ⰴ������ʱ��������ʾ�¶ȵ���ʱ
            if (key_flag) {
                openpower_show_temp_time = 300; // 3�룬������ѭ��10msһ��
            }
			if(EventListener(KEY_POWER_LONG))
			{
				MachineStat = MODE_OUT_OPEN_POWER;
				openpower_code.open_power_code_flag=0;
				OpenPower_Reset();
				Buzzer_key();
			}
			else if(EventListener(KEY_POWER_SHORT))
			{
				openpower_code.open_power_code_flag=openpower_code.open_power_code_flag+1;
				if(openpower_code.open_power_code_flag==2)
				{
					display_openpowercode(100,100,100);
				}
				else if(openpower_code.open_power_code_flag>2)
				{
					display_openpowercode1(100);
				}
				Buzzer_key();
			}
			else if(EventListener(KEY_BUZZER_LONG))
			    BuzzerKeyFunc();

		//aaron��4lines 2. ��ʾ�߼�����
//        if(openpower_show_temp_time > 0) {
//                // ��ʾ�¶�
//            display_run(heater_info.SetHeatTemp, heater_info.eco_stat, heater_info.fire_stat, heater_info.akey_cycle, heater_info.super_mode,
//                            heater_info.running_mode, heater_info.bathtub_mode, heater_info.cool_and_hot);
//        }else{
            open_power_code_process();
			if(openpower_code.open_power_code_flag==0)
			{
				if(blink_flag_1000ms)
				{
					display_openpowercode2();
				}
				else
				{
					display_Temperature_Clean();
				}
			}
			else if(openpower_code.open_power_code_flag==1)//���������?
			{
				//openpower_code.Seed=openpower_flag%100;
				if(blink_flag_1000ms)
				{
					display_openpowercode2();
				}
				else
				{
					display_Temperature_Clean();
				}
			}
			else if(openpower_code.open_power_code_flag==2)//��ʾ�����?
			{
				display_openpowercode(openpower_code.suiji_code[0],openpower_code.suiji_code[1],openpower_code.suiji_code[2]);
			}
			else if(openpower_code.open_power_code_flag==3)
			{
				display_openpowercode1(openpower_code.input_code[2]);
			}
			else if(openpower_code.open_power_code_flag==4)
			{
				display_openpowercode1(openpower_code.input_code[1]);
			}
			else if(openpower_code.open_power_code_flag==5)
			{
				display_openpowercode1(openpower_code.input_code[0]);
			}
			else if(openpower_code.open_power_code_flag>=6)
			{
                if(openpower_code.open_power_success==1){
                    MachineStat = MODE_OUT_OPEN_POWER;
				}else if(openpower_code.open_power_success==OPEN_POWER_TEMP){ 
					open_power_max_time = OPEN_POWER_FTY_TIMEOUT;  
                    OpenPowerLock = 0;
					MachineStat = MODE_OUT_OPEN_POWER;
					openpower_code.open_power_code_flag = 0;
					OpenPower_Reset(); 
				}else{	
					openpower_code.open_power_code_flag = 1;
					OpenPower_Reset();					
				}
			}
			
			if((openpower_code.open_power_code_flag>=3)&&(openpower_code.open_power_code_flag<=5))	
			{
				if(EventListener(KEY_DECREASE_SHORT))
				{
					Buzzer_key();
					num_change=(openpower_code.input_code[5-openpower_code.open_power_code_flag])%10;
					openpower_code.input_code[5-openpower_code.open_power_code_flag]=openpower_code.input_code[5-openpower_code.open_power_code_flag]-num_change;
					num_change=num_change-1;
					
					if(num_change>10)
					{
						num_change=9;
					}
					openpower_code.input_code[5-openpower_code.open_power_code_flag]=openpower_code.input_code[5-openpower_code.open_power_code_flag]+num_change;
				}
				else if(EventListener(KEY_INCREASE_SHORT))
				{
					Buzzer_key();
					openpower_code.input_code[5-openpower_code.open_power_code_flag]=openpower_code.input_code[5-openpower_code.open_power_code_flag]+10;
					if(openpower_code.input_code[5-openpower_code.open_power_code_flag]>99)
					{
						openpower_code.input_code[5-openpower_code.open_power_code_flag]=openpower_code.input_code[5-openpower_code.open_power_code_flag]-100;
					}
				}
			}
	//	}	
		break;
#endif
		case MODE_POWEROFF:
		{
			display_clean();//DisplayDriverClear();

			if(EventListener(KEY_POWER_SHORT)){
				if(OpenPowerLock == 1){
					MachineStat = MODE_OPEN_POWER;
				}else{
					key_flag=1;//标记位，表示有按键按下，需要发送数�?
					key_num=1;//通信数据�?
				}
				Buzzer_key();
			}	
			if(heater_info.power_stat==0x31){
				MachineStat=MODE_RUN;
			}
			
			Err_Dis_Stat=STA_0;
			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
			}
		}
		break;
		case MODE_RUN:
			//mode_change_flag
#ifdef OPEN_POWER_FLAG
			if(openpower_code.open_power_success != OPEN_POWER_OK){
				if(open_power_max_time<=openpower_time.time_min) 
				{
					OpenPowerLock = 1;
					MachineStat=MODE_OPEN_POWER;
					break;
				}else
				    OpenPowerLock = 0;
			}

#endif
			if(mode_change_flag==0)
			{
				if(key_flag==0)
				{
					mode_save_temp[heater_info.running_mode]=heater_info.Send_SetHeatTemp;
				}
				
			}
			else if(mode_change_flag==1)
			{
				if(key_flag==0)
				{
					//key_flag=1;
					//key_num=3;
					mode_change_flag=0;
					heater_info.Send_SetHeatTemp=mode_save_temp[heater_info.running_mode];
				}
			}
			if(EventListener(KEY_POWER_SHORT)){
				key_flag=1;
				key_num=1;
				Buzzer_key();
			}
#ifdef OPEN_POWER_FLAG
			else if(EventListener(KEY_POWER_LONG))
			{
				if(openpower_code.open_power_success !=OPEN_POWER_OK)
				{
					openpower_code.open_power_code_flag = 1;
					MachineStat = MODE_OPEN_POWER;
					Buzzer_key();
				}
			}
#endif
			else if(EventListener(KEY_ECO_SHORT)){
				key_flag=1;
				key_num=2;
				Buzzer_key();
			}
			else if(EventListener(KEY_INCREASE_SHORT)){
				if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMax)
				{
					key_flag=1;
					key_num=3;
					if(heater_info.Send_SetHeatTemp<TEMP_CHANGE)
					{
						if(heater_info.Send_SetHeatTemp>=TEMP_DECREASE_1)
						{
							heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_5;
						}
						else if(heater_info.Send_SetHeatTemp==TEMP_320)
						{
							heater_info.Send_SetHeatTemp=TEMP_DECREASE_1;
						}
					}
					else if(heater_info.Send_SetHeatTemp==TEMP_CHANGE)
					{
						heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_10;
					}
					else if(heater_info.Send_SetHeatTemp>TEMP_CHANGE){
						heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_10;
					}
				}
				Buzzer_key();
			}	
			else if(EventListener(KEY_DECREASE_SHORT)){
				
				if(heater_info.SetHeatTempMin<heater_info.Send_SetHeatTemp)
				{
					key_flag=1;
					key_num=3;
					if(heater_info.Send_SetHeatTemp>TEMP_CHANGE)
					{
						heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_10;
					}
					else if((heater_info.Send_SetHeatTemp<=TEMP_CHANGE)&&(heater_info.Send_SetHeatTemp>TEMP_DECREASE_1))
					{
						heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_5;
					}
					else if(heater_info.Send_SetHeatTemp==TEMP_DECREASE_1)
					{	
						heater_info.Send_SetHeatTemp=TEMP_320;
					}
					// if(heater_info.Send_SetHeatTemp>TEMP_CHANGE)
					// {
					// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_10;
					// }
					// else if(heater_info.Send_SetHeatTemp==TEMP_CHANGE)
					// {
					// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_5;
					// }
					// else if(heater_info.Send_SetHeatTemp<TEMP_CHANGE)
					// {
					// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_5;
					// }
					//heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMin;
				}
				Buzzer_key();
			}
			else if(EventListener(KEY_CYCLE_SHORT))
			{
				MB_SetCommand(4);
				CycleActiveSrc = CYCLE_SRC_KEY;
				Buzzer_key();
			}
			else if(EventListener(KEY_PREUSS_SHORT)){	
                MB_SetCommand(6);
				Buzzer_key();	
			}
			else if(EventListener(KEY_INCREASE_LONG)){
				
				if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMax)
				{
					//self_in=1;
					if(Get_Time(UnitMSec,&touchtime)>LONG_KEY_TIME)
					{
						Get_Time(TIME_CLEAN,&touchtime);
						key_flag=1;
						key_num=3;
						if(heater_info.Send_SetHeatTemp==TEMP_320)
						{
							heater_info.Send_SetHeatTemp=TEMP_DECREASE_1;
						}
						else if(heater_info.Send_SetHeatTemp<TEMP_INCREASE)
						{
							heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_10;
						}
						else if(heater_info.Send_SetHeatTemp==TEMP_INCREASE)
						{
							heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_5;
						}
						else if(heater_info.Send_SetHeatTemp>TEMP_INCREASE_1){
							//heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp+INCREASE_TEMP_10;
						}
						
					}	
				}
				else
				{
					Get_Time(TIME_CLEAN,&touchtime);
				}
				
			}
			else if(EventListener(KEY_BUZZER_LONG))	
                BuzzerKeyFunc(); 
			else if(EventListener(KEY_DECREASE_LONG)){
				
				if(heater_info.Send_SetHeatTemp>heater_info.SetHeatTempMin)
				{
					//self_de=1;
					if(Get_Time(UnitMSec,&touchtime)>LONG_KEY_TIME)
					{
						Get_Time(TIME_CLEAN,&touchtime);
						key_flag=1;
						key_num=3;
						if(heater_info.Send_SetHeatTemp>TEMP_DECREASE_1)
						{
							heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_10;
						}
						if(heater_info.Send_SetHeatTemp<TEMP_DECREASE_1)
						{
							heater_info.Send_SetHeatTemp=TEMP_DECREASE_1;
						}
						// if(heater_info.Send_SetHeatTemp>TEMP_DECREASE)
						// {
						// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_10;
						// }
						// else if(heater_info.Send_SetHeatTemp==TEMP_DECREASE)
						// {
						// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-DECREASE_TEMP_5;
						// }
						// else if(heater_info.Send_SetHeatTemp<TEMP_DECREASE_1)
						// {
						// 	heater_info.Send_SetHeatTemp=heater_info.Send_SetHeatTemp-TEMP_DECREASE_1;
						// }
					}
				}
				
				else 
				{
					Get_Time(TIME_CLEAN,&touchtime);
				}
				// if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMin){
				// 	heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMin;
				// }	
				
			}
			else if(EventListener(KEY_MODE_SHORT))
			{
				if(heater_info.running_mode==RUN_PLAIN)
				{
					if(heater_info.bathtub_mode==0x00)
					{
						if(heater_info.cool_and_hot==0x31)
						{
							//cool and hot
							key_flag=1;
							key_num=0x0d;
							heater_info.cool_and_hot=0x30;
						}
						else if(heater_info.cool_and_hot==0x30)
						{
							key_flag = 1;
	    	                key_num = 7;
                            SetRunMode(RUN_KITCHEN);
							
						}
					}
					else /* 退出浴缸模式 */
					{
						key_flag=1;
						key_num=0x0b;
						heater_info.bathtub_mode = 0;
					}
					
				}
				else if(heater_info.running_mode==RUN_KITCHEN)
				{ 
                    SetRunMode(RUN_SHOWER);
					key_flag = 1;
	    	        key_num = 7;
				}
				else if(heater_info.running_mode==RUN_SHOWER) /* 切换到冷热冲模式 */
				{
					key_flag=1;
					key_num=0x0d;
					SetRunMode(RUN_PLAIN);
					heater_info.cool_and_hot=0x31; 
				}
				mode_change_flag=1;
				Buzzer_key();	
			}
			else
			{
				Get_Time(TIME_CLEAN,&touchtime);
			}

			//if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMin)
			//{
			//	key_flag=1;
			//	key_num=3;
			//	heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMin;
			//}
			//else if(heater_info.Send_SetHeatTemp>heater_info.SetHeatTempMax)
			//{
			//	key_flag=1;
			//	key_num=3;
			//	heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMax;
			//}
			if(heater_info.power_stat==0x30){
				MachineStat=MODE_POWEROFF;
				//break;
			}
			else if(heater_info.err_type==0x02){
				MachineStat=MODE_ERR;
			}
			else if(heater_info.err_type==0x00)
			{
#ifdef OPEN_POWER_FLAG
				if(openpower_code.open_power_success==OPEN_POWER_NONE || openpower_code.open_power_success==OPEN_POWER_TEMP)
				{
					Get_Time(UnitSec,&errtime);
					switch(Err_Dis_Stat)
					{
						case STA_0:
							display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
							if(err_time_cnt>300){
								err_time_cnt=0;
								Get_Time(TIME_CLEAN,&errtime);
                            if(three_time==0)
                            {
                                Err_Dis_Stat=STA_1;
                            }else
                            {
                                Err_Dis_Stat=STA_0;
                                
                            }                                
								
							}
						break;
						case STA_1:
                            //display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
                            
							display_Temperature_Clean();
							//if(errtime.time_sec>0.1){
							if(err_time_cnt>100){
								err_time_cnt=0;
								Get_Time(TIME_CLEAN,&errtime);
								Err_Dis_Stat=STA_2;
							}
						break;			
						case STA_2:
                            //display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);                        
							display_openpowercode2();
							if(err_time_cnt>300){
								err_time_cnt=0;
								Get_Time(TIME_CLEAN,&errtime);
								Err_Dis_Stat=STA_3;
							}			
						break;
						case STA_3:
                            //display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
                            
							display_Temperature_Clean();
							//if(errtime.time_sec>0.1){
								if(err_time_cnt>100){
									err_time_cnt=0;
								Get_Time(TIME_CLEAN,&errtime);
								Err_Dis_Stat=STA_0;
							}
						break;
					}			
				}
				else if(openpower_code.open_power_success!=OPEN_POWER_NONE)
				{
					display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
				}
#else
				display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
					,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
#endif
			}
			else if(heater_info.err_type==0x01)
			{
				Get_Time(UnitSec,&errtime);
				switch(Err_Dis_Stat)
				{
					case STA_0:
						display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
						if(err_time_cnt>1000){
							err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_1;
//							if(three_time==0)
//							{
//									Err_Dis_Stat=STA_1;
//							}else
//							{
//									Err_Dis_Stat=STA_0;
//									
//							}          							
						}
					break;
					case STA_1:
						display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);                        
						display_Temperature_Clean();
						if(err_time_cnt>50){
							err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_2;
						}
					break;			
					case STA_2:
						display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
						if(err_time_cnt>200){
							err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_3;
						}			
					break;
					case STA_3:
						display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);                        
						    display_Temperature_Clean();
							if(err_time_cnt>50){
								err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
                            if((three_time==0)&&(openpower_code.open_power_success==0))
                            {
                                Err_Dis_Stat=STA_4;
                            }else
                            {
                                Err_Dis_Stat=STA_0;
                                
                            }
							
						}
					break;
                    case STA_4:
                        display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);
                        display_openpowercode2();
                            if(err_time_cnt>400){
                                err_time_cnt=0;
                            Get_Time(TIME_CLEAN,&errtime);
                            Err_Dis_Stat=STA_5;                    
                          }
                            break;
                    case STA_5: 
						display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode\
				,heater_info.running_mode,heater_info.bathtub_mode,heater_info.cool_and_hot);                        
                           display_Temperature_Clean();
                            if(err_time_cnt>50){
                                err_time_cnt=0;
                            Get_Time(TIME_CLEAN,&errtime);
                            Err_Dis_Stat=STA_0;  
                      
                  }                    
                            break;                            
				}			
			}

			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
			}
			//display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
			
			
		break;
			
	    case MODE_ERR:
			Get_Time(UnitSec,&errtime);
			
			if(EventListener(KEY_POWER_SHORT))
			{
				key_flag=1;
				key_num=1;
				Buzzer_clear();
				ErrKeyBuzzerDelay = 20;
			}
			if(heater_info.power_stat==0x30){
				MachineStat=MODE_POWEROFF;
			}
			
			else if(heater_info.err_type==0){
				MachineStat=MODE_RUN;
			}
			else if(heater_info.err_type==0x02){				
				
				switch(fire_err_stat)
				{
					case STA_FIRE_0:
						display_err_fire(heater_info.err_code_l_h,heater_info.err_code_l_l);
						//if(errtime.time_sec>1.5){
						if(err_time_cnt>300){
							err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
							fire_err_stat=STA_FIRE_1;
						}
					
					break;
					case STA_FIRE_1:
						err_display_clean(heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode,heater_info.running_mode,heater_info.cool_and_hot);
						//if(errtime.time_sec>0.5){	
						if(err_time_cnt>100){
							err_time_cnt=0;
							Get_Time(TIME_CLEAN,&errtime);
							fire_err_stat=STA_FIRE_0;
						}
					break;
				}
				
				//if(blink_flag_1000ms){
				//	display_err_fire(heater_info.err_code_h);
				//}
			}
						
			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
			}
		break;

		case MODE_WIFI:
			
			 
		break;
		
		case MODE_COMM_ERR:
			display_clean();
			if(Commfail_Flag==0){
				MachineStat=COMM_ERR_LAST_MODE;
			}
		break;			
		case MODE_FACTORY_TEST:
             MachineStat=MODE_FACTORY_TEST;
             break;
        case MODE_FACTORY:
                     
		     break;

		case MODE_VERSION:
			Get_Time(UnitSec,&versiontime);
			//display_run(Rinnai_version,0x31,0x01,0x31,0x31);
			display_version(Rinnai_letter_version, Rinnai_number_version,0x31,0x01,0x30,0x30);
			if(versiontime.time_sec>10)
			{
				Get_Time(TIME_CLEAN,&versiontime);
				MachineStat=version_comm;
				version_comm=MODE_TEST;
			}
			if(EventListener(KEY_POWER_LONG)||EventListener(KEY_POWER_SHORT))
			{
				MachineStat=version_comm;
				version_comm=MODE_TEST;
				Buzzer_key();
			}
		break;
			
        default:
            break;
	}
 
	if(MachineStat_old != MachineStat) mode_change=1;
	else  mode_change=0; 
	if(mode_change){
	    Get_Time(TIME_CLEAN ,&timeout);//timeout=0;
		Get_Time(TIME_CLEAN,&errtime);
 		err_time_cnt=0;
	}

	if( IsBathtubeFinish() && bBathtubeFinish == 0){
		Buzzer_Bathtube();
	}else if(heater_info.err_type==0x00)
	{
		//err_time_cnt=0;
		if(err_flag!=heater_info.err_type)
		{
				Buzzer_clear();
				err_time_cnt=0;
		}
		err_flag=heater_info.err_type;
	}
	else if(heater_info.err_type==0x02)
	{
		if(MachineStat!=MODE_INIT)
		{
			
			if(MachineStat==MODE_POWEROFF)
			{
				Buzzer_clear_err_in_poweroff();
			}
			if(err_flag!=heater_info.err_type)
			{
				//err_flag=heater_info.err_type;
				Buzzer_err();
			}
			err_flag=heater_info.err_type;
		}
	}
	/* 浴缸加水满状态更新 */
	bBathtubeFinish = IsBathtubeFinish();

	MachineStat_old=MachineStat;
	
//	if (button_stat!=0)
//	{
//		wifi_ctrl_block_time = 150;
//	}
//	if (wifi_ctrl_block_time>0)
//	{
//		wifi_ctrl_block_time--;
//	}
    if(three_3_flg)
    {
        three_time = 300;
        three_3_flg = 0;
    }
    if(three_time>0)
    {
        three_time-=1;
    }
	CycleTask();	
	
	if(ErrKeyBuzzerDelay != 0) ErrKeyBuzzerDelay--;
	if(ErrKeyBuzzerDelay == 1) Buzzer_key();
}
#if 1 /* 预约及一键循环相关 */
uint8_t IsOrderActive(void){
uint8_t flag = 0;
uint8_t hour = time_stat.day_hour;

    if(hour < 8)
		flag = CycleOrder.TimeTable[0]&(0x80>>hour);
	else if(hour<16)
		flag = CycleOrder.TimeTable[1]&(0x80>>(hour-8));
	else if(hour<24)
        flag = CycleOrder.TimeTable[2]&(0x80>>(hour-16));
    return flag;
}

void CycleTask(void){ 
static uint8_t bOrderCheck = 0; 
uint8_t hour = time_stat.day_hour;

    Get_Time(UnitHour, &cycle_time);
    if(CycleOrder.Enable == 0x31){
         if(time_stat.day_min == 0 && time_stat.day_sec == 0 && bOrderCheck == 0)  /* 整点判断预约 */
             bOrderCheck = 1;
         else if(time_stat.day_sec != 0)
             bOrderCheck = 0;
         
         if(bOrderCheck == 1){
             bOrderCheck = 2;
             if(IsOrderActive() && heater_info.send_akey_cycle == 0x30){
                    MB_SetCommand(4);
			 }else if(IsOrderActive()==0 && heater_info.send_akey_cycle == 0x31){
				   	MB_SetCommand(4);
			 }	
		 }else{	  
             if(heater_info.send_akey_cycle == 0x31){
				if(IsOrderActive())
				   ;
				else {
					if(cycle_time.time_min >= CycleDuration){
						MB_SetCommand(4); 
					}
				}
			 }      
		 }
	}else{
		if(heater_info.send_akey_cycle == 0x31){
			if(cycle_time.time_min >= CycleDuration){
				MB_SetCommand(4);  
			}
		}
	}
}
#endif


/* 设定运行模式, 针对厨房和淋浴模式设定温度范围 */
void SetRunMode(uint8_t run_mode){
    if(run_mode == RUN_PLAIN){
		heater_info.Send_SetHeatTemp=mode_save_temp[run_mode];
		/* 普通模式下, 最高温度由主板设定 */			
        heater_info.running_mode = RUN_PLAIN;
	}else if(run_mode==RUN_KITCHEN){
		heater_info.SetHeatTempMax=420;
		heater_info.SetHeatTempMin=350;		
		heater_info.Send_SetHeatTemp=mode_save_temp[run_mode];
        heater_info.running_mode = RUN_KITCHEN;
	}else if(run_mode==RUN_SHOWER) {
		heater_info.SetHeatTempMax=450;
		heater_info.SetHeatTempMin=350;	
		heater_info.Send_SetHeatTemp=mode_save_temp[run_mode];
        heater_info.running_mode = RUN_SHOWER;
	}  
	mode_change_flag=1;  
}


/** @brief 蜂鸣器按键功能 */
void BuzzerKeyFunc(void){
	if(buzzer_stat==BUZZER_OFF){
		Buzzer_open();
	}else if(buzzer_stat==BUZZER_ON){
		Buzzer_close();
	}
}

/** 判断WIFI是否被锁定,锁定则屏蔽WIFI的F4命令
 *  ex: 开机码超时锁定，面板按键3秒锁定 
 * */ 
uint8_t IsLock(void){
	if(OpenPowerLock == 1) return 1;
	if(three_time !=0) return 1;
	return 0;
}

/** @brief 是否是自检模式，开机后先进入自检模式 */
uint8_t IsSelfCheckMode(void){
	return MachineStat == MODE_INIT;
}

/** @brief 浴缸是否放水结束 */
uint8_t IsBathtubeFinish(void){
    if(heater_info.power_stat == 0x30) return 0;
	if(heater_info.bathtub_mode==0x63||heater_info.bathtub_mode==0x62||heater_info.bathtub_mode==0x61||heater_info.bathtub_mode==0x44)
	   return 1;
	return 0;   
}

void EnterFactoryMode(uint8_t fact){
	FactoryMode = fact;
}

uint8_t IsFactoryMode(void){
    return FactoryMode == 0x31;
}
